
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ScrewTurn.Wiki.PluginFramework;

namespace ScrewTurn.Wiki {

	public partial class SessionRefresh : BasePage {

		protected void Page_Load(object sender, EventArgs e) {
			// Manage the case when the application is for some reason restarted
			// during the editing of a Page or File

			if(Request["Page"] != null) {
				PageInfo page = Pages.Instance.FindPage(Request["Page"]);
				if(page == null) return;
				else {
					if(page.Status != PageStatus.Public) {
						if(SessionFacade.LoginKey == null && Request.Cookies[Settings.LoginCookieName] != null) {
							// There is not the risk of an endless redirect loop because
							// Login.aspx removes the cookie if it's not valid, and therefore this block
							// is executed only one time
							Response.Redirect("/Members/Login.aspx?Redirect=SessionRefresh.aspx" + (Request["Page"] != null ? Tools.UrlEncode("?Page=" + Request["Page"]) : ""));
						}
					}
					string username = Request.UserHostAddress;
					if(SessionFacade.LoginKey != null) username = SessionFacade.Username;
					Collisions.Instance.Renew(page, username);
				}
			}

		}

		public void PrintRefresh() {
			Response.Write(@"<meta http-equiv=""refresh"" content=""");
			Response.Write(Collisions.RefreshInterval.ToString());
			Response.Write(@""" />");
		}

	}

}
